package com.example.planetexploder.basepresenter;

import android.content.Context;
import android.util.Log;

import com.example.planetexploder.bean.maindata.BookInfo;
import com.example.planetexploder.bean.maindata.ItemInfo;
import com.example.planetexploder.constants.LogConst;
import com.example.planetexploder.constants.SortConst;
import com.example.planetexploder.model.dao.Dao;
import com.example.planetexploder.model.folder.Folder;
import com.example.planetexploder.model.preference.SharedPreference;
import com.example.planetexploder.utils.TimeUtil;

import java.util.ArrayList;

/**
 * Presenter层摘抄集操作
 * @author 宋熙然
 * 修改日期：2019.10.23
 */
public class PBook {

    private static final String TAG = "PBook";

    private Dao dao;
    private Folder folder;
    private SharedPreference sp;

    public PBook(Context context) {
        dao = new Dao(context);
        folder = new Folder(context);
        sp = new SharedPreference(context);
    }

    /**
     * 获得摘抄集表中的信息
     * @param bookId 摘抄集id
     * @param order  排序方式
     * @see SortConst 排序方式相关常量
     * @return       摘抄集表中的信息
     */
    public ArrayList<ItemInfo> get_BookTable(String bookId, int order) {
        Log.d(LogConst.PTAG, "get_BookTable()");
        return dao.bookTable.get_BookTable(bookId, order);
    }

    /**
     * 删除摘抄项
     * @param bookInfo 摘抄集信息
     * @param itemId 摘抄项id
     */
    public void delete_Item(BookInfo bookInfo, String itemId) {
        Log.d(LogConst.PTAG, "delete_Item()");
        String time = TimeUtil.getCurrentTime();
        //dao 删除摘抄项信息
        dao.bookTable.delete_ItemInfo_From_BookTable(bookInfo.bookId, itemId);
        //folder 删除文件
        if(ItemInfo.isText(itemId))
            folder.textFile.delete_Text(bookInfo.bookId,itemId);
        else folder.imgFile.delete_ImgGroupFolder(bookInfo.bookId, itemId);
        //dao 更新该摘抄集的信息（修改日期）
        bookInfo.dateMod = time;
        dao.shelfTable.update_BookInfo(bookInfo);
        sp.appSettings.set_FirstPageBook(bookInfo.bookId);
    }

}
